package com.weichat.controller;

import com.weichat.converter.OrderFormToOrderDTOConverter;
import com.weichat.dto.OrderDTO;
import com.weichat.exception.SellException;
import com.weichat.form.OrderForm;
import com.weichat.service.OrderService;
import com.weichat.util.ResultEnum;
import com.weichat.util.ResultVOUtil;
import com.weichat.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA
 * Created By HZP
 * Date: 2018/6/19
 * Time: 22:27
 */
@Controller
@Slf4j
@RequestMapping("/buyer/order")
public class BuyerOrderController {

    @Autowired
    private OrderService orderService;

    @RequestMapping("/index")
    public String index(){

        return "1212121";
    }

    //创建订单
    @RequestMapping("/create")
    @ResponseBody
    public ResultVO<Map<String,String>> create(@Valid OrderForm orderForm,
                                               BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            log.error("创建订单 参数不对");
            throw new SellException(ResultEnum.PARAM_ERROR.getCode(),
                    bindingResult.getFieldError().getDefaultMessage()
                    );
        }
        OrderDTO orderDTORequest= OrderFormToOrderDTOConverter.converter(orderForm);
        if(orderDTORequest.getOrderDetails()==null){
            log.error("创建订单 购物车不能为空");
            throw new SellException(ResultEnum.CART_EMPTY);
        }

        OrderDTO orderDTO=orderService.create(orderDTORequest);
        Map<String,String> map=new HashMap<>();
        map.put("orderId",orderDTO.getOrderId());
        return ResultVOUtil.success(map);
    }

    //订单列表
    @RequestMapping("/list")
    @ResponseBody
    public ResultVO<List<OrderDTO>> list(HttpServletRequest request) {
        String openid = request.getParameter("openid");
        int page = request.getParameter("page") != null
                ? Integer.parseInt(request.getParameter("page")):0;

        int size = request.getParameter("size") != null
                ? Integer.parseInt(request.getParameter("size")):10;
        if (StringUtils.isEmpty(openid)) {
            log.error("查询订单列表 opetnid 不为空");
            throw new SellException(ResultEnum.PARAM_ERROR);
        }
        PageRequest requestPage = new PageRequest(page, size);
        Page<OrderDTO> orderDTOPage = orderService.findList(openid, requestPage);
        return ResultVOUtil.success(orderDTOPage.getContent());
    }


    //订单详情
    @ResponseBody
    @RequestMapping("/detail")
    public ResultVO<OrderDTO> detail(@RequestParam("openid") String openid,
                                     @RequestParam("orderId") String orderId){
        OrderDTO orderDTO=orderService.findOne(orderId);

        return ResultVOUtil.success(orderDTO);
    }

    //取消订单
    @RequestMapping("/cancel")
    public ResultVO cancel(@RequestParam("openid") String openid,
                           @RequestParam("orderId") String orderId){
        OrderDTO orderDTO=orderService.findOne(orderId);
        orderService.cancle(orderDTO);
        return ResultVOUtil.success(orderDTO);
    }


}
